package crawler;

import java.util.List;

import asa.PostBucket;

import parser.ParserPipeline;

import forum.ForumFactory;
import forum.ForumPost;

public abstract class Crawler extends Thread {

	protected ForumFactory fac;
	protected ParserPipeline pp;
	protected PostBucket crawlerBucket;

	public Crawler(String name, ForumFactory fac, ParserPipeline pp,
			PostBucket crawlerBucket) {
		super("Crawler-" + name);
		this.fac = fac;
		this.pp = pp;
		this.crawlerBucket = crawlerBucket;
	}

	/**
	 * Gather a thread and put it into the bucket.
	 */
	protected abstract List<ForumPost> crawl() throws Exception;

	public void run() {
		while (canCrawl()) {
			try {
				List<ForumPost> posts = crawl();
				if (posts == null)
					continue;
				crawlerBucket.addPost(posts);
			} catch (InterruptedException ie) {
				break;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			crawlerBucket.finish();
			close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.err.println("Crawler finished.");
	}

	public abstract void close() throws Exception;

	public abstract boolean canCrawl();
}
